package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Teacher;
import com.ruoyi.system.service.ITeacherService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 老师Controller
 * 
 * @author ruoyi
 * @date 2024-06-03
 */
@Controller
@RequestMapping("/system/teacher")
public class TeacherController extends BaseController
{
    private String prefix = "system/teacher";

    @Autowired
    private ITeacherService teacherService;

    @RequiresPermissions("system:teacher:view")
    @GetMapping()
    public String teacher()
    {
        return prefix + "/teacher";
    }

    /**
     * 查询老师列表
     */
    @RequiresPermissions("system:teacher:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Teacher teacher)
    {
        startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 导出老师列表
     */
    @RequiresPermissions("system:teacher:export")
    @Log(title = "老师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Teacher teacher)
    {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        return util.exportExcel(list, "老师数据");
    }

    @Log(title = "老师", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:teacher:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        List<Teacher> teacherList = util.importExcel(file.getInputStream());
        String message = teacherService.importTeacher(teacherList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @Log(title = "老师", businessType = BusinessType.CLEAN)
    @RequiresPermissions("system:teacher:remove")
    @PostMapping("/clean")
    @ResponseBody
    public AjaxResult clean()
    {
        teacherService.cleanTeacher();
        return success();
    }

    /**
     * 新增老师
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存老师
     */
    @RequiresPermissions("system:teacher:add")
    @Log(title = "老师", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Teacher teacher)
    {
        return toAjax(teacherService.insertTeacher(teacher));
    }

    /**
     * 修改老师
     */
    @RequiresPermissions("system:teacher:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Teacher teacher = teacherService.selectTeacherById(id);
        mmap.put("teacher", teacher);
        return prefix + "/edit";
    }

    /**
     * 修改保存老师
     */
    @RequiresPermissions("system:teacher:edit")
    @Log(title = "老师", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Teacher teacher)
    {
        return toAjax(teacherService.updateTeacher(teacher));
    }

    /**
     * 删除老师
     */
    @RequiresPermissions("system:teacher:remove")
    @Log(title = "老师", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(teacherService.deleteTeacherByIds(ids));

    }

    @RequiresPermissions("system:teacher:remove")
    @Log(title = "老师", businessType = BusinessType.CLEAN)
    @GetMapping("/refreshCache")
    @ResponseBody
    public AjaxResult refreshCache()
    {
        teacherService.resetTeacherCache();
        return success();
    }
}
